package com.zhz.livegoods.comment.service.impl;

import com.zhz.livegoods.comment.mapper.CommentMapper;
import com.zhz.livegoods.comment.service.CommentService;
import com.zhz.livegoods.commons.vo.LivegoodsResult;
import com.zhz.livegoods.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**商品评论服务实现
 * @author zhz
 * @date 2020/08/05
 **/
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 根据商品ID，分页查询商品评论信息
     * @param itemId 商品主键
     * @param page 查询第几页
     * @param rows 每页多少行
     * @return
     */
    @Override
    public LivegoodsResult findCommentByItemId(String itemId, int page, int rows) {
        //创建查询条件
        Query query=new Query();
        query.addCriteria(
                Criteria.where("itemId").is(itemId)
        );
        query.with(//分页逻辑
                PageRequest.of(page,rows)
        );
        //分页查询商品评论
        List<Comment> commentList = commentMapper.findCommentsByPage(query);
        long count = commentMapper.countByQuery(itemId);
        //隐藏用户手机号,12345678912->123****8912
        for (Comment comment : commentList) {
            comment.getUsername().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
        }
        LivegoodsResult result=LivegoodsResult.ok(commentList);
        //总计页码数
        long totalPages = (count % rows == 0) ? (count / rows) : ((count / rows) + 1);
        if ((page+1)<totalPages){//有更多数据
            result.setHasMore(true);
        }else {//没有更多数据
            result.setHasMore(false);
        }
        return result;
    }
}
